package com.wenjh.jdmzt.android.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.wenjh.jdmzt.AndroidKit;
import com.wenjh.jdmzt.R;
import com.wenjh.jdmzt.util.StringUtils;

public class MAlertDialog extends Dialog implements View.OnClickListener {

	private List<Button> mButtons;
	private int dialogId;
	private boolean titileDividerShow = true;  // 是否显示title分割线
	
	private View titleLayout = null;
	private ViewGroup contentLayout = null;
	private View buttonLayout = null;
	private View titleDividerView = null;

	private boolean cancelable = true;
	private boolean canceledOnTouchOutside = false;
	
	private boolean clickAfterDismiss = true;
	
	
	public MAlertDialog(Context paramContext) {
		super(paramContext, R.style.Theme_Light_CustomDialog);
		super.setContentView(R.layout.common_dialog_generic);
		
		mButtons = new ArrayList<Button>();
		Button button1 = (Button) findViewById(R.id.dilaog_button1);
		button1.setOnClickListener(this);
		this.mButtons.add(button1);
		
		Button button2 = (Button) findViewById(R.id.dilaog_button2);
		button2.setOnClickListener(this);
		this.mButtons.add(button2);
		
		Button button3 = (Button) findViewById(R.id.dilaog_button3);
		button3.setOnClickListener(this);
		this.mButtons.add(button3);
		
		titleLayout = findViewById(R.id.dialog_layout_title);
		contentLayout = (ViewGroup) findViewById(R.id.dialog_layout_content);
		buttonLayout = findViewById(R.id.dialog_layout_button);
		titleDividerView = findViewById(R.id.dialog_title_divider);
		
		setCancelable(true);
		setCanceledOnTouchOutside(true);
		setTitle(R.string.dialog_title);
		
		findViewById(R.id.dialog_layout_root).setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (cancelable && canceledOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN) {
		            cancel();
		            return true;
		        }
				
				return false;
			}
		});
	}

	@Override
	public void setCancelable(boolean cancelable) {
		super.setCancelable(cancelable);
		this.cancelable = cancelable;
	}
	
	@Override
	public void setCanceledOnTouchOutside(boolean cancel) {
		super.setCanceledOnTouchOutside(cancel);
		this.canceledOnTouchOutside = cancel;
	}
	
	private int getWhichButton(View button) {
		for (int i = 0; i < mButtons.size(); i++) {
			if (mButtons.get(i) == button) {
				return i;
			}
		}
		return -1;
	}
	
	public void setContentView(View view) {
		if(contentLayout.getChildCount() > 0) {
			contentLayout.removeAllViews();
		}
		contentLayout.addView(view);
	}
	
	@Override
	public void setContentView(int layoutResID) {
		setContentView(AndroidKit.getLayoutInflater().inflate(layoutResID, null));
	}
	
	@Override
	public void setContentView(View view, LayoutParams params) {
		if(contentLayout.getChildCount() > 0) {
			contentLayout.removeAllViews();
		}
		contentLayout.addView(view, params);
	}
	
	/**
	 * 获取当先对话框根元素
	 * @return
	 */
	public View getContentView(){
		return contentLayout;
	}

	/**
	 * 构建一个对话框实例
	 * 
	 * @param context
	 * @param msgResid
	 *            对话框消息内容的资源id (R.String)
	 * @param btn1TextResid
	 *            对话框按钮(1)文本的资源id (R.String)paramInt
	 * @param btn2TextResid
	 *            对话框按钮(2)文本的资源id (R.String)
	 * @param btn1ClickListener
	 *            对话框按钮(1) 的点击事件监听器
	 * @param btn2ClickListener
	 *            对话框按钮(2) 的点击事件监听器
	 * @return
	 */
	public static MAlertDialog makeConfirm(Context context, int msgResid,
			int btn1TextResid, int btn2TextResid,
			DialogInterface.OnClickListener btn1ClickListener,
			DialogInterface.OnClickListener btn2ClickListener) {
		return makeConfirm(context, context.getString(msgResid), AndroidKit.getString(btn1TextResid), AndroidKit.getString(btn2TextResid), btn1ClickListener, btn2ClickListener);
	}
	
	public static MAlertDialog makeConfirm(Context context, CharSequence message,
			CharSequence btn1Text, CharSequence btn2Text,
			DialogInterface.OnClickListener btn1ClickListener,
			DialogInterface.OnClickListener btn2ClickListener) {
		MAlertDialog dialog = new MAlertDialog(context);
		dialog.setMessage(message);
		dialog.setButton(0, btn1Text, btn1ClickListener);   
		dialog.setButton(1, btn2Text, btn2ClickListener);
		dialog.setConfimStyle();
		
		return dialog;
	}
	
	public MAlertDialog addButtonListener(int index,DialogInterface.OnClickListener clickListener) {
		Button button = mButtons.get(index);
		button.setTag(clickListener);
		return this;
	}
	
	/**
	 * 设置对话框样式为“确认”样式。
	 * “确认” 按钮使用不同的背景颜色
	 * @return
	 */
	public MAlertDialog setConfimStyle() {
//		Button button = (Button) this.mButtons.get(0);
//		button.setBackgroundResource(R.drawable.btn_default_submit);
		return this;
	}

	/**
	 * 构建一个默认的对话框实例。此对话框实例将附带一个 “确认”、“取消” 按钮。 使用者需要传递“确认”按钮的点击事件监听实例。
	 * 
	 * @param context
	 * @param msgResid
	 *            对话框消息内容
	 * @param confirmButtonClickListener
	 *            "确认" 按钮的点击事件监听器
	 * @param cancelButtonClickListener "取消"按钮的点击事件监听器
	 * @return
	 */
	public static MAlertDialog makeConfirm(Context context, CharSequence message,
			DialogInterface.OnClickListener confirmButtonClickListener, DialogInterface.OnClickListener cancelButtonClickListener) {
		return makeConfirm(context, message, context.getString(R.string.confim),
				context.getString(R.string.cancle), confirmButtonClickListener, cancelButtonClickListener);
	}
	
	public static MAlertDialog makeConfirm(Context context, int id,
			DialogInterface.OnClickListener confirmButtonClickListener, DialogInterface.OnClickListener cancelButtonClickListener) {
		return makeConfirm(context, AndroidKit.getString(id), context.getString(R.string.confim),
				context.getString(R.string.cancle), confirmButtonClickListener, cancelButtonClickListener);
	}
	
	/**
	 * 构建一个默认的对话框实例。此对话框实例将附带一个 “确认”、“取消” 按钮。 使用者需要传递“确认”按钮的点击事件监听实例。
	 * 
	 * @param context
	 * @param msgResid
	 *            对话框消息内容
	 * @param onClickListener
	 *            "确认" 按钮的点击事件监听器
	 * @return
	 */
	public static MAlertDialog makeConfirm(Context context, CharSequence message,
			DialogInterface.OnClickListener onClickListener) {
		return makeConfirm(context, message, context.getString(R.string.confim),
				context.getString(R.string.cancle), onClickListener, null);
	}
	
	/**
	 * 构建一个默认的对话框实例。此对话框实例将附带一个 “确认”、“取消” 按钮。 使用者需要传递“确认”按钮的点击事件监听实例。
	 * 
	 * @param context
	 * @param msgResid
	 *            对话框消息内容的资源id (R.String)
	 * @param onClickListener
	 *            "确认" 按钮的点击事件监听器
	 * @return
	 */
	public static MAlertDialog makeConfirm(Context context, int msgResid,
			DialogInterface.OnClickListener onClickListener) {
		return makeConfirm(context, msgResid, R.string.confim,
				R.string.cancle, onClickListener, null);
	}
	
	/**
	 * 创建单个按钮的对话框
	 * @param context
	 * @param message
	 * @param onClickListener
	 * @return
	 */
	public static MAlertDialog makeSingleButtonDialog(Context context, CharSequence message,
			DialogInterface.OnClickListener onClickListener) {
		MAlertDialog dialog = new MAlertDialog(context);
		dialog.setMessage(message);
		dialog.setButton(0, context.getString(R.string.confim), onClickListener);
		
		return dialog;
	}
	
	/**
	 * 创建单个按钮的对话框
	 * @param context
	 * @param message
	 * @param btnText 按钮文字
	 * @param onClickListener
	 * @return
	 */
	public static MAlertDialog makeSingleButtonDialog(Context context, CharSequence message, String btnText,
			DialogInterface.OnClickListener onClickListener) {
		MAlertDialog dialog = new MAlertDialog(context);
		dialog.setMessage(message);
		dialog.setButton(0, btnText, onClickListener);
		
		return dialog;
	}
	
	public static MAlertDialog makeSingleButtonDialog(Context context, int msgTextId, int btnTextId,
			DialogInterface.OnClickListener onClickListener) {
		return makeSingleButtonDialog(context, AndroidKit.getString(msgTextId), AndroidKit.getString(btnTextId), onClickListener);
	}
	
	/**
	 * 创建单个按钮的对话框
	 * @param context
	 * @param resId
	 * @param onClickListener
	 * @return
	 */
	public static MAlertDialog makeSingleButtonDialog(Context context, int resId,
			DialogInterface.OnClickListener onClickListener) {
		return makeSingleButtonDialog(context, context.getString(resId), onClickListener);
	}

	public int getDialogId() {
		return this.dialogId;
	}

	/**
	 * 触发按钮的点击事件。
	 * 
	 * @param button
	 *            按钮对象
	 */
	public void onClick(View button) {
		DialogInterface.OnClickListener onClickListener = (DialogInterface.OnClickListener) button
				.getTag();
		if (onClickListener != null) {
			int i = getWhichButton(button);
			onClickListener.onClick(this, i);
		}
		
		if(clickAfterDismiss) {
			dismiss();
		} else {
			clickAfterDismiss = true;
		}
	}

	public Button setButton(int index, int textResid,
			DialogInterface.OnClickListener onClickListener) {
		return setButton(index, getContext().getString(textResid), onClickListener);
	}

	public Button setButton(int index, CharSequence text,
			DialogInterface.OnClickListener onClickListener) {
		
		Button button = (Button) this.mButtons.get(index);
		button.setText(text);
		button.setVisibility(View.VISIBLE);
		addButtonListener(index, onClickListener);
		
		buttonLayout.setVisibility(View.VISIBLE);
		
		return button;
	}

	public void setDialogId(int id) {
		this.dialogId = id;
	}

	public void setMessage(int resId) {
		setMessage(getContext().getString(resId));
	}

	public void setMessage(CharSequence text) {
		((TextView) findViewById(R.id.dialog_tv_message)).setText(text);
	}

	public void setTitle(int resId) {
		setTitle(getContext().getString(resId));
	}

	public void setTitle(CharSequence text) {
		if(text == null || StringUtils.isEmpty(text.toString())) {
			titleLayout.setVisibility(View.GONE);
			return;
		}
		
		((TextView) findViewById(R.id.dialog_tv_title)).setText(text);
		titleLayout.setVisibility(View.VISIBLE);
		
		if(!titileDividerShow) {
			titleDividerView.setVisibility(View.GONE);
		} else {
			titleDividerView.setVisibility(View.VISIBLE);
		}
	}
	
	public void setTitleDividerShown(boolean show) {
		this.titileDividerShow = show;
		
		if(titleLayout.getVisibility() == View.VISIBLE) {
			if(show) {
				titleDividerView.setVisibility(View.VISIBLE);
			} else if(!show && !titleLayout.isShown()) {
				titleDividerView.setVisibility(View.GONE);
			}
		} 
	}
	
	/**
	 * 拦截按钮点击后对话框的关闭事件。 ---- 点击按钮不关闭对话框。
	 * 此属性只在 “本次或下一次” 按钮的点击事件中生效一次。
	 * 
	 */
	public void interceptDismiss() {
		this.clickAfterDismiss = false;
	}
}
